/*
编写 : Hexh
时间 : 2012-11-29 15:12:54
*/
using System.Collections.Generic;
using HMIS.FrameWork.DomainBase;
using HMIS.FrameWork.Repository.Nhb;
using HMIS.Infrastructure.Core.Entities;

namespace HMIS.Infrastructure.Core.Repositories.Impl
{
	/// <summary>
	///	 the repository of InfraApproveHistep
	/// </summary>
	public class InfraApproveHisStepRepositoryImpl : RepositoryNhbImpl<InfraApproveHisStep>, IInfraApproveHisStepRepository
	{

		/// <summary>
		/// 判断是否已经有重复值
		/// </summary>
		/// <param name="fieldName">行名</param>
		/// <param name="fieldValue">值</param>
		/// <param name="id">ID列的值</param>
		/// <returns>True， False</returns>
		public bool IsFieldExist(string fieldName, string fieldValue, string id)
		{
			return base.IsFieldExist(fieldName, fieldValue, id, null);
		}
		
		// <summary>
		/// 判断是否已经有重复值
		/// </summary>
		/// <param name="fieldName">行名</param>
		/// <param name="fieldValue">值</param>
		/// <param name="id">ID列的值</param>
		/// <param name="where">Where 条件</param>
		/// <returns>True， False</returns>
		public bool IsFieldExistWhere(string fieldName, string fieldValue, string id, string where)
		{
			return base.IsFieldExist(fieldName, fieldValue, id, where);
		}
	
		/// <summary>
		/// 查询 InfraApproveHistep
		/// </summary>
		/// <param name="query">查询条件 审批流程ID</param>
		/// <param name="start">分页开始</param>
		/// <param name="limit">分页大小</param>
		/// <param name="sort">排序条件</param>
		/// <param name="dir">排序方向</param>
		/// <param name="filters">筛选条件</param>
		/// <param name="total">返回记录条数</param>
		/// <returns>返回记录集</returns>
		public IList<InfraApproveHisStep> GetPlist(string approveHisId, int start, int limit, string sort, string dir, List<DataFilter> filters, out long total)
		{
			// filter
			var strFilter = base.GetHqlstrByExtFilter(filters, "t");
			
			string strQuery = "( t.ApproveHisId = :approveHisId ) " 
					+ (string.IsNullOrEmpty(strFilter) ? string.Empty : " and " + strFilter);

			// sort
			string strSort = string.IsNullOrEmpty(sort) ? string.Empty : " order by t." + sort + " " + dir;
			string strSql = @" from InfraApproveHisStep as t where " + strQuery;

			// 查询
			var selQuery = Session.CreateQuery(@"select t " + strSql + strSort).SetString("approveHisId", approveHisId);
			var totQuery = Session.CreateQuery(@"select count(*) " + strSql).SetString("approveHisId", approveHisId);
			// 分页
			if (limit > 0)
				selQuery.SetFirstResult(start).SetMaxResults(limit);

			// 汇总并返回
			total = totQuery.UniqueResult<long>();
			return selQuery.List<InfraApproveHisStep>();
		}
	}
}
